package com.ymk.cmd;

import com.ymk.base.cmd.ACmd;
import com.ymk.base.cmd.CmdSign;
import com.ymk.base.utlis.ParamsBuilder;
import com.ymk.core.code.cmd.RbacCmdCode;
import com.ymk.bo.rbac.RbacPermBO;
import com.ymk.service.IRbacPermService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class RbacPermListCmd extends ACmd {

    @Override
    public CmdSign executeSop(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        RbacPerm reqData = super.getData(sign, RbacPerm.class);
        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                .addParam("id", reqData.getId())
                .addParam("type", reqData.getType())
                .addParam("parentId", reqData.getParentId())
                .setReturnStruct(sign.getReturnStruct());

        List<RbacPermBO> rbacPermBOList = rbacPermService.list(paramsBuilder.build());
        respSign.setSource(super.getSource(rbacPermBOList));
        return respSign;
    }

    @Override
    public CmdSign executeMop(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        RbacPerm reqData = super.getData(sign, RbacPerm.class);
        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                .addParam("id", reqData.getId())
                .addParam("type", reqData.getType())
                .addParam("parentId", reqData.getParentId())
                .setReturnStruct(sign.getReturnStruct());

        List<RbacPermBO> rbacPermBOList = rbacPermService.list(paramsBuilder.build());
        respSign.setSource(super.getSource(rbacPermBOList));
        return respSign;
    }

    @Override
    public boolean anon(CmdSign sign) {
        return true;
    }

    @Override
    public String getCmdCode() {
        return RbacCmdCode.RBAC_PERM_LIST;
    }

    @Autowired
    private IRbacPermService rbacPermService;

}
